package controllers;

import java.net.MalformedURLException;
import java.net.URL;

import models.User;


import com.google.appengine.tools.appstats.AppstatsServlet;
import com.google.gdata.client.appsforyourdomain.UserService;
import com.google.gdata.client.authn.oauth.GoogleOAuthParameters;
import com.google.gdata.client.authn.oauth.OAuthHmacSha1Signer;
import com.google.gdata.data.appsforyourdomain.provisioning.UserEntry;
import com.google.gdata.data.appsforyourdomain.provisioning.UserFeed;

import play.Logger;
import play.i18n.Lang;
import play.mvc.Http;
import utils.constants.SessionConstants;


public class ApplicationController extends BaseController {
	/**
	 * Renders the main page of the application.
	 */
	public static void index() {
        render();
    }
	
	/**
	 * Renders the login page of the application.
	 */
	public static void login() {
        render();
	}
	
	public static void marketSetup() {
		String domain = params.get("domain");
		User manager = getCurrentUser();
		if(manager == null) {
			flash.put("from", "google");
			flash.put("domain", domain);
			
			flash.put("redirect", "/setup");
			MarketController.login();
		} else {
			//ManagerController.getGoogleSetup(); // TODO
		}
	}
	
	/**
	 * Removes the user, and some session data like the domain and redirects to the index.
	 */
	public static void logout() {
		session.current().put(SessionConstants.USER, null);
		session.current().put(SessionConstants.DOMAIN, null);
		
		redirect("/"); // TODO The best would be to call the method index. But at this moment I'm getting a cyclic redirection, so it doesn't work.
	}
	
	/**
	 * Change the language of the application and redirects to the page that you came from.
	 */
	public static void language(String lang) {
    	Lang.change(lang);
    	
    	String refererURL =	Http.Request.current().headers.get("referer").value();
    	redirect(refererURL); 
    }
	
	public static void google_apps() {
		render();
	}
}